////////////////////////////////////////////////////////////////////////////////
//
// $Id$ 
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
// This software consists of voluntary contributions is licensed under the 
// MIT <http://www.opensource.org/licenses/mit-license.php>. 
// 
// 
// Portuguese Translation
// 
// ESTE SOFTWARE É FORNECIDO “COMO ESTÁ”, SEM GARANTIAS DE QUALQUER ESPÉCIE, 
// EXPRESSAS OU TÁCITAS, INCLUINDO SEM LIMITAÇÕES, QUAISQUER GARANTIA DE COMERCIABILIDADE, 
// DE ADEQUAÇÃO A FINALIDADES ESPECÍFICAS OU DE NÃO VIOLAÇÃO A DIREITOS DE TERCEIROS. 
// EM NENHUMA HIPÓTESE OS AUTORES OU TITULARES DOS DIREITOS AUTORAIS SERÃO RESPONSÁVEIS 
// POR QUAISQUER RECLAMAÇÕES E/OU INDENIZAÇÕES POR DANOS OU POR OUTROS MOTIVOS, 
// SEJAM ESSES DERIVADOS DE OBRIGAÇÕES CONTRATUAIS OU DE DECISÕES JUDICIAIS, 
// ILÍCITOS CIVIS OU OUTROS MOTIVOS, OS QUAIS VENHAM A OCORRER EM CONSEQÜÊNCIA 
// OU EM RELAÇÃO AO SOFTWARE , A SEU USO OU OUTRA FORMA DE NEGOCIAÇÃO/UTILIZAÇÃO. 
// 
// Esse software consiste de contribuições voluntárias e está sobre a licença
// MIT <http://www.opensource.org/licenses/mit-license.php>.
// 
////////////////////////////////////////////////////////////////////////////////

package br.mcaslib.swiz.view
{
	import br.mcaslib.swiz.event.CrudEvent;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Text;
	import mx.events.InvalidateRequestData;
	
	import spark.components.TextInput;
	import spark.components.supportClasses.SkinnableComponent;
	
	[SkinState("normal")]
	[SkinState("detail")]
	[SkinState("update")]
	[SkinState("add")]
	[Event(name="CrudAction", type="br.mcaslib.swiz..event.CrudEvent")]
	
	public class CrudToolbar extends SkinnableComponent implements ICrudToolbar
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function CrudToolbar() {
			super();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		[SkinPart(false)]
		public var detailSkinPart:Button;
		
		[SkinPart(false)]
		public var createSkinPart:Button;
		
		[SkinPart(false)]
		public var updateSkinPart:Button;
		
		[SkinPart(false)]
		public var deleteSkinPart:Button;
		
		[SkinPart(false)]
		public var refreshSkinPart:Button;
		
		[SkinPart(false)]
		public var quickFilterSkinPart:Text;
		
		[SkinPart(false)]
		public var filterSkinPart:Button;
		
		public var crudState:String;
		
		[Embed(source="/assets/images/filter.png")]
		[Bindable]
		public var imgFilter:Class;
		
		[Embed(source="/assets/images/filter_apply.png")]
		[Bindable]
		public var imgFilterApply:Class;
		
		//----------------------------------
		//  filterOn
		//----------------------------------
		
		private var _filterOn:Boolean;
		
		public function set filterOn( value:Boolean ):void {
			this._filterOn = value;
			this.updateFilter();
		}
		
		public function get filterOn():Boolean {
			return this._filterOn;
		}

		//----------------------------------
		//  quickFilterOn
		//----------------------------------
		
		private var _quickFilterOn:Boolean;
		
		public function set quickFilterOn( value:Boolean ):void {
			this._quickFilterOn = value;
			this.updateFilter();
		}
		
		public function get quickFilterOn():Boolean {
			return this._quickFilterOn;
		}
		
		//----------------------------------
		//  filterApplied
		//----------------------------------
		
		private var _filterAppliedOn:Boolean;
		
		public function set filterAppliedOn( value:Boolean ):void {
			this._filterAppliedOn = value;
			this.updateFilter();
		}
		
		public function get filterAppliedOn():Boolean {
			return this._filterAppliedOn;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Handler methods
		//
		//--------------------------------------------------------------------------
		
		protected function skinPart_handlerClick( event:MouseEvent ):void {
			var crudEvent:CrudEvent = new CrudEvent( CrudEvent.EVENT_CRUD_ACTION );
			crudEvent.action = event.target.data;
			this.dispatchEvent( crudEvent ); 
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden methods
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  ICrudToolbar
		//----------------------------------

		public function setCrudState( value:String ):void {
			this.crudState = value
			this.invalidateSkinState();
		}
		
		public function turnFilter( value:Boolean ):void {
			this.filterOn = value;
		}
		
		public function turnQuickFilter( value:Boolean ):void {
			this.quickFilterOn = value;
		}
		
		public function filterApplied( value:Boolean ):void {
			this.filterAppliedOn = value;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 *  @inheritDoc
		 */
		override protected function partAdded(partName:String, instance:Object):void {
			super.partAdded(partName, instance);
			
			if (instance == detailSkinPart) {
				detailSkinPart.addEventListener( MouseEvent.CLICK, skinPart_handlerClick );
				detailSkinPart.data = CrudEvent.ACTION_DETAIL;
			}
			
			if (instance == createSkinPart) {
				createSkinPart.addEventListener( MouseEvent.CLICK, skinPart_handlerClick );
				createSkinPart.data = CrudEvent.ACTION_CREATE;
			}
			
			if (instance == updateSkinPart) {
				updateSkinPart.addEventListener( MouseEvent.CLICK, skinPart_handlerClick );
				updateSkinPart.data = CrudEvent.ACTION_UPDATE;
			}
			
			if (instance == deleteSkinPart) {
				deleteSkinPart.addEventListener( MouseEvent.CLICK, skinPart_handlerClick );
				deleteSkinPart.data = CrudEvent.ACTION_DELETE;
			}
			
			if (instance == refreshSkinPart) {
				refreshSkinPart.addEventListener( MouseEvent.CLICK, skinPart_handlerClick );
				refreshSkinPart.data = CrudEvent.ACTION_REFRESH;
			}
			
			if (instance == quickFilterSkinPart) {
				this.updateFilter();
			}
			
			if (instance == filterSkinPart) {
				filterSkinPart.addEventListener( MouseEvent.CLICK, skinPart_handlerClick );
				filterSkinPart.data = CrudEvent.ACTION_FILTER;
				this.updateFilter();
			}
		}

		/**
		 *  @inheritDoc
		 */
		override protected function partRemoved(partName:String, instance:Object):void {
			super.partAdded(partName, instance);
			
			if (instance == detailSkinPart) {
				detailSkinPart.removeEventListener( MouseEvent.CLICK, skinPart_handlerClick );
			}
			if (instance == createSkinPart) {
				createSkinPart.removeEventListener( MouseEvent.CLICK, skinPart_handlerClick );
			}
			if (instance == updateSkinPart) {
				updateSkinPart.removeEventListener( MouseEvent.CLICK, skinPart_handlerClick );
			}
			if (instance == deleteSkinPart) {
				deleteSkinPart.removeEventListener( MouseEvent.CLICK, skinPart_handlerClick );
			}
			if (instance == refreshSkinPart) {
				refreshSkinPart.removeEventListener( MouseEvent.CLICK, skinPart_handlerClick );
			}
			if (instance == filterSkinPart) {
				filterSkinPart.removeEventListener( MouseEvent.CLICK, skinPart_handlerClick );
			}
		}
		
		override protected function getCurrentSkinState():String {
			return this.crudState;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Protected/Private methods
		//
		//--------------------------------------------------------------------------

		private function updateFilter():void {
			if (quickFilterSkinPart) {
					quickFilterSkinPart.includeInLayout = true;
				if( this.quickFilterOn ) {
					quickFilterSkinPart.visible = true;
				} else {
					quickFilterSkinPart.includeInLayout = false;
					quickFilterSkinPart.visible = false;
				} 
			}

			if (filterSkinPart) {
				if( this.filterOn ) {
					filterSkinPart.includeInLayout = true;
					filterSkinPart.visible = true;
					if( this.filterAppliedOn ) {
						filterSkinPart.setStyle( 'icon', imgFilterApply );
					} else {
						filterSkinPart.setStyle( 'icon', imgFilter );
					}
					
				} else {
					filterSkinPart.includeInLayout = false;
					filterSkinPart.visible = false;
					
				} 
			}
		}
	}
}
